package com.ineatconseil.yougo.pages;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.dozer.Mapper;

import com.ineatconseil.yougo.beans.ActionType;
import com.ineatconseil.yougo.services.auth.Authenticator;
import com.ineatconseil.yougo.services.rest.RestServiceInvoker;
import com.sun.jersey.api.client.ClientResponse;

/**
 * Comme properties for all pages.
 * 
 * @author johanblo
 * 
 */
public abstract class AbstractPage {

	@Inject
	private RestServiceInvoker restServiceInvoker;

	@Inject
	private Mapper mapper;

	@Inject
	private Authenticator authenticator;

	@Inject
	private Messages msgFile;

	@Persist(PersistenceConstants.FLASH)
	@Property
	private String message;

	protected Mapper getMapper() {
		return mapper;
	}

	protected RestServiceInvoker getRestServiceInvoker() {
		return restServiceInvoker;
	}

	protected static boolean isEmptyParameter(String value) {
		return value == null || "".equals(value) || "0".equals(value) || "null".equals(value);
	}

	public Authenticator getAuthenticator() {
		return authenticator;
	}

	public void setActionMessageOk(String itemType, ActionType actionType) {
		this.message = this.msgFile.get(itemType + "-" + actionType.getCode() + "-ok");
	}

	public String getRESTErrorMessage(ClientResponse.Status status) {
		if (status == null) {
			return this.msgFile.get("default-rest-error-msg");
		}
		String msg = this.msgFile.get(status.name() + "-error-msg");
		if (msg == null || "".equals(msg)) {
			return this.msgFile.get("default-error-msg");
		}
		return msg;
	}

}
